/* FileBusiness.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   JOHN_BENNY	2008-7-9	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/** 文件操作类 */
public class FileBusiness {
    private static String packagePath = "";
    static String packAgeName = "JWXT";
    private static String projectName = "GW";
    private static String realPath = "";
    static String mapFile = "file";
    static String mapIsFile = "isFile";
    static String mapPath = "path";
    static String mapSub = "sub";
    static String mapFileName = "fileName";
    
    /** 获得绝对路径 */
    private static String getPath() {
        URL url = ClassLoader.getSystemClassLoader().getSystemResource("//");
        packagePath = url.getPath();
        return packagePath;
    }

    /** 解析路径 得到实际需要的路径JWXT */
    public static String getRealPath() {
        FileBusiness.getPath();
        String way = packagePath.substring(1);
        String[] arr = way.split("/");
        int size = arr.length;
        for (int i = 0; i < size; i++) {
            if (arr[i].equals(projectName)) {
                realPath = linkStr(i, arr);
            }
        }
        return realPath;
    }

    /** 判断是否已存在此文件或文件夹 */
    public static boolean isFileExists(String path) {
        File file = new File(path);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    /** 连接路径 */
    private static String linkStr(int i, String[] arr) {
        String way = "";
        for (int j = 0; j <= i; j++) {
            way += arr[j] + "/";
        }
        way += packAgeName + "/";
        return way;
    }

    /** 解析 获得 文件夹下的所有文件及文件夹 */
    public static Vector getFiles(String path) {
        Vector vec = null;
        try {
            File file = new File(path);
            // 如果存在此文件夹
            if (file.exists()) {
                vec = new Vector();
                // 获取所有的文件/文件夹名称
                String[] str = file.list();
                for (int i = 0; i < str.length; i++) {
                    String strPath = path + str[i];
                    // 创建文件对象
                    File fl = new File(strPath);
                    Map map = new HashMap();
                    // 获得是否是文件的标示
                    boolean flg = fl.isFile();
                    //map.put(FileBusiness.mapFile, fl);
                    map.put(FileBusiness.mapIsFile, String.valueOf(flg));
                    // 如果是文件
                    if (flg) {
                        System.out.println("-该文件是->" + path + str[i]);
                        map.put(FileBusiness.mapPath, path + str[i]);
                        map.put(FileBusiness.mapFileName, str[i]);
                    } else {
                        System.out.println("-该文件夹是->" + path + str[i]);
                        map.put(FileBusiness.mapPath, path + str[i] + "/");
                        map.put(FileBusiness.mapFileName, str[i]);
                        String[] strP = fl.list();
                        Vector vec1 = new Vector();
                        for (int j = 0; j < strP.length; j++) {
                            Map map1 = new HashMap();
                            String strPath1 = strPath + "/" + strP[j];
                            File fl1 = new File(strPath1);
                            boolean flg1 = fl1.isFile();
                            //map.put(FileBusiness.mapFile, fl1);
                            map1.put(FileBusiness.mapIsFile, String.valueOf(flg1));
                            System.out.println("--子一级的文件名称->>" + strPath1);
                            map1.put(FileBusiness.mapPath, strPath1);
                            map1.put(FileBusiness.mapFileName, strP[j]);
                            vec1.add(map1);
                        }
                        map.put(FileBusiness.mapSub, vec1);
                    }
                    // System.out.println("-->"+str[tI]);
                    vec.add(map);
                }
            } else {
                System.out.println("没有文件夹或数据");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return null;
        }
        return vec;
    }

    /** 创建EXCEL文件 */
    public static boolean createExcelFile(String path, HSSFWorkbook book, String fileName) {
        boolean result = false;
        try {
            FileOutputStream fos = new FileOutputStream(path + fileName);
            book.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        result = true;
        return result;
    }

    /** 创建文件夹 */
    static boolean createDocument(String path) {
        File file = new File(path);
        boolean result = file.mkdir();
        if (result == true) {
            System.out.println("Create successfully!");
            System.out.println("创建文件夹");
        } else {
            System.out.println("Disable to make the folder,please check the disk is full or not.");
            System.out.println(" 文件夹创建失败，清确认磁盘没有写保护并且空件足够");
        }
        return result;
    }
    
    /** 根据路径提取单一文件*/
    public static HSSFWorkbook getFile(Vector vec,String fileName,HSSFWorkbook wb){
        int size = vec.size();
        String filename = null;
        String isFile = null;
        InputStream myxls = null;
        for (int i = 0; i < size; i++) {
            Map map = (Map)vec.get(i);
            filename = String.valueOf(map.get(FileBusiness.mapFileName));
            isFile = String.valueOf(map.get(FileBusiness.mapIsFile));
            if(isFile.equals("false")){
                Vector vecSub = (Vector)map.get(FileBusiness.mapSub);
                wb = getFile(vecSub, fileName,wb);
            }
            if(filename.equals(fileName)){
                try {
                    myxls = new FileInputStream(String.valueOf(map.get(FileBusiness.mapPath)));
                    wb = new HSSFWorkbook(myxls);
                } catch (FileNotFoundException e) {
                    
                    e.printStackTrace();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            }
        }
        return wb;
    }
}
